<?php
namespace Umpmobile\Controller;
use Think\Controller;
class IndexController extends ExtendController {
	public $catinfo;
	public $friend;
	public $friend_id;
	public $my_nick;
	public $my_id;
	public $my_info;
	public function index(){
		$where = $this->cid?" AND `id`=".$this->cid:"";
		$CATS = M("PromoCats");
		$cats = $CATS->where("`sid`=".$this->sid.$where)->getField("`id`,`name`, `friend_num`, `decrease_amount`, `promo_count`, `start_time`, `end_time`, `comments`");
		if(count($cats)==1){//如果只有1条，跳转到单活动页面
			$this->catinfo = current($cats);
			$this->event();
			die();
		}
		if(count($cats)==0){
			$this->display("none");
			die();
		}
		
		$ITEM = M("Items");
		$items = $ITEM->where("`sid`=".$this->sid)->getField("`id`,`num_iid`, `title`, `recommend`, `activity_id`, `promo_id`, `pic_url`");
		foreach($items as $k=>$item){
			$goods[$item["num_iid"]]["item"]=$item;
			$goods[$item["num_iid"]]["events"][]=$ev;
		}
		$this->assign("goods",$goods);
		$this->assign("cats",$cats);
		$this->assign("ts",time());
		$this->display("index");
	}
	public function event(){
		$EVENT = M("Promosions");
		if($this->catinfo){
			// 如果有分类信息，则直接读取分类内的活动
			$event = $EVENT->where("`sid`=".$this->sid." AND `act_cat_id`=".$this->catinfo["id"])->order("`friend_num`")->getField("`id`,`name`, `participate_range`, `start_time`, `end_time`, `is_user_tag`, `user_tag`,`decrease_amount`,`friend_num`, `vip_user`, `sum_pay`, `pay_time_start`, `pay_time_end`, `item_iids`, `once_pay`, `status`, `promo_count`");
		}elseif($this->cid){
			// 没有分类信息，则按照给出的分类ID读取活动
			$CATS = M("PromoCats");
			$this->catinfo = current($CATS->where("`sid`=".$this->sid." AND `id`=".$this->cid)->getField("`id`,`name`, `friend_num`, `decrease_amount`, `promo_count`, `start_time`, `end_time`, `comments`"));
			$event = $EVENT->where("`sid`=".$this->sid." AND `act_cat_id`=".$this->cid)->order("`friend_num`")->getField("`id`,`name`, `participate_range`, `start_time`, `end_time`, `is_user_tag`, `user_tag`,`decrease_amount`,`friend_num`, `vip_user`, `sum_pay`, `pay_time_start`, `pay_time_end`, `item_iids`, `once_pay`, `status`, `promo_count`");
		}else{
			header("HTTP/1.0 404 Not Found");//使HTTP返回404状态码
			$this->none();
			die();
		}
		
		// $friendnick = urldecode(I("friendnick"));//我邀请朋友，以我为主视角，我的昵称由URL传递
		$friendnick = I("friendnick");//我邀请朋友，以我为主视角，我的昵称由URL传递。TthinkPHP已经进行过解码，所以不再自行解码：
		
		$FRI = D("Friends");
		$FRI->sid=$this->sid;
		$FRI->cid=$this->cid;
		$FRI->friend=$this->mixednick;
		$this->my_id = $FRI->getFriend(); //取出自己的ID
		$this->my_info = $FRI->friend_info; //暂存自己的朋友信息

		
		$INV = D("Invite");
		$INV->sid=$this->sid;
		$INV->home_info = $this->my_info;
		$INV->catid = $this->cid;
		if($friendnick){ //有朋友信息的情况下才判断邀请逻辑
			$FRI->friend=$friendnick;
			$this->friend_id = $FRI->getFriend(); //取出朋友的ID
			$this->friend_info = $FRI->friend_info; //暂存朋友信息
			
			$INV->gueset_info = $this->friend_info;
			$INV->invite(); //邀请逻辑，达到指定朋友数量后，打标签。
		}
		
		$code_img = $INV->get_code_img();
		
		foreach($event as $k=>$e){
			$eids[]=$k;
			$need = $e["friend_num"]-$this->my_info["friends"];
			$event[$k]["need_friends"]=$need;
			if($need>0){
				$event[$k]["friend_info"] = "还需要请".$need."人即可打折，加油哦！";
			}else{
				$neer_evnet = $e;
				$event[$k]["friend_info"] = "已成功享受折扣！";
			}
		}
		if(!$neer_evnet){
			$neer_evnet = $event[$eids[0]];
		}
		// print_r($neer_evnet);
		$ITEM = M("Items");
		$items = $ITEM->where("`sid`=".$this->sid." AND `promo_id` = ".$neer_evnet["id"])->limit(0,2)->getField("`id`,`num_iid`, `title`, `price`, `recommend`, `activity_id`, `promo_id`, `pic_url`");
		foreach($items as $k=>$item){
			$items[$k]["neer_evnet"]=$neer_evnet;
			if($neer_evnet["decrease_amount"]>0 && $items[$k]["friends"]>0 && $items[$k]["friends"]>=$neer_evnet["friend_num"]){
				$items[$k]["prom_price"] = ($items[$k]["price"] - $neer_evnet["decrease_amount"])/100;
			}else{
				$items[$k]["prom_price"] = $items[$k]["price"]/100;
			}
			$items[$k]["price"] =  $items[$k]["price"]/100;
		}
		
		$SHIP = D("Friendships");
		$last_invite_time = $SHIP->where("`sid`=".$this->sid." AND `home_id`=".$this->my_id)->limit(1)->getField("invite_time");;
		
		$this->assign("share_link","https://hongbao.ews.m.jaeapp.com/?friendnick=".urlencode($this->mixednick)."&sid=".$this->sid."&cid=".$this->cid); //分享链接
		$this->assign("last_invite_time",$last_invite_time); 
		$this->assign("cur_prom",$this->cur_prom);
		$this->assign("my_id",$this->my_id); 
		$this->assign("my_info",$this->my_info); 
		$this->assign("friend",$friendnick); 
		$this->assign("goods",$goods);
		$this->assign("items",$items);
		$this->assign("cats",$this->catinfo);
		$this->assign("code_img",$code_img);
		$this->assign("event",$event);
		$this->display("event");
	}
	public function get_cur_items(){
		$start = I("start");
		$perload = I("perload");
		$EVENT = M("Promosions");
		$event = $EVENT->where("`sid`=".$this->sid." AND `act_cat_id`=".$this->cid)->getField("`id`,`name`, `participate_range`, `start_time`, `end_time`, `is_user_tag`, `user_tag`,`decrease_amount`,`friend_num`, `vip_user`, `sum_pay`, `pay_time_start`, `pay_time_end`, `item_iids`, `once_pay`, `status`, `promo_count`");
		$FRI = D("Friends");
		$FRI->sid=$this->sid;
		$FRI->cid=$this->cid;
		$FRI->friend=$this->mixednick;
		$this->my_id = $FRI->getFriend(); //取出自己的ID
		$this->my_info = $FRI->friend_info; //暂存自己的朋友信息
		foreach($event as $k=>$e){
			$eids[]=$k;
			$need = $e["friend_num"]-$this->my_info["friends"];
			$event[$k]["need_friends"]=$need;
			if($need>0){
				$event[$k]["friend_info"] = "还需要请".$need."人即可打折，加油哦！";
			}else{
				$neer_evnet = $e;
				$event[$k]["friend_info"] = "已成功享受折扣！";
			}
		}
		if(!$neer_evnet){
			$neer_evnet = $event[$eids[0]];
		}
		$ITEM = M("Items");
		$items = $ITEM->where("`sid`=".$this->sid." AND `promo_id` = ".$neer_evnet["id"])->limit(($start?$start:0),($perload?($start+$perload):5))->getField("`id`,`num_iid`, `title`, `price`, `recommend`, `activity_id`, `promo_id`, `pic_url`");
		$count = $ITEM->where("`sid`=".$this->sid." AND `promo_id` = ".$neer_evnet["id"])->count("`id`");
		foreach($items as $k=>$item){
			$items[$k]["neer_evnet"]=$neer_evnet;
			$items[$k]["friends"] = $this->my_info["friends"];

			if($neer_evnet["decrease_amount"]>0 && $items[$k]["friends"]>0 && $items[$k]["friends"]>=$neer_evnet["friend_num"]){
				$items[$k]["prom_price"] = ($items[$k]["price"] - $neer_evnet["decrease_amount"])/100;
			}else{
				$items[$k]["prom_price"] = $items[$k]["price"]/100;
			}
			 $items[$k]["price"] =  $items[$k]["price"]/100;
		}
		$re = new \stdClass();
		$re->items=$items;
		$re->all=$count;
		$this->ajaxReturn($re,"JSON");
	}
	public function none(){
		$this->display();
	}
}